var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient
var config = require('../config/db')
var passport=require('passport')

/* 查询某订单的图书信息 */
router.get('/getOrdersDetailsInfo', function (req, res, next) {
    //1.接受请求的用户编号参数
    var orederDeatilId = parseInt(req.query.orderIdDeatil)
    //2.连接数据库
    MongoClient.connect(config.url, function (err, client) {
        if (err) {
            console.log('数据库连接失败', err)
            res.json({ code: 200, message: 'error', data: err })
        } else {
            //3.获取用户的所有订单
            var db = client.db()
            db.collection('order_detail').aggregate([
                {
                    $lookup:
                    {
                        from: "order",
                        localField: "order_id",
                        foreignField: "order_id",
                        as: "order"
                    }
                },
                {
                    $lookup:
                    {
                        from: "books",
                        localField: "books_id",
                        foreignField: "books_id",
                        as: "books"
                    }
                },
                {
                    $match:{order_detail_id:'1'}
                    // $match: { order_id: orderDeatilId }
                },
                { $unwind: '$books' },
                { $unwind: '$order' }
            ]).toArray(function (err, result) {
                if (err) {
                    console.log('数据获取失败！', err)
                } else {
                    res.json({ code: 200, message: 'ok', data: result })
                    client.close()
                }
            })

        }
    })
});

module.exports = router;